package cn.cjlmonster.demo02.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;
import android.widget.TextView;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;

import cn.cjlmonster.demo02.R;

/**
 * This project is Demo02
 * Created by cjl on 2021/11/20 12:47 上午.
 */

public abstract class MyMarkerView extends MarkerView {

    private TextView tv_msg;
    private Paint paint;

    public MyMarkerView(Context context) {
        super(context, R.layout.layout_marker);
        tv_msg = findViewById(R.id.tv_msg);
        paint = new Paint();
        paint.setColor(Color.parseColor("#1C1C1C"));
    }

    @Override
    public void refreshContent(Entry e, Highlight highlight) {
//        tv_msg.setText(e.getY() + " - " + highlight.getX());
        onItemSelected((int) e.getX(), tv_msg);
        super.refreshContent(e, highlight);
    }

    protected abstract void onItemSelected(int xPos, TextView msg);

    @Override
    public MPPointF getOffsetForDrawingAtPoint(float posX, float posY) {
//        return super.getOffsetForDrawingAtPoint(posX, posY);
        return getOffset();
    }

    private MPPointF mOffset;

    @Override
    public MPPointF getOffset() {

        if (mOffset == null) {
            // center the marker horizontally and vertically
//            mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
            mOffset = new MPPointF(0, 0);
        }

        return mOffset;
    }


    @Override
    public void draw(Canvas canvas, float posX, float posY) {
        Log.i("画图", "draw: posX = " + posX + ", posY = " + posY + ", " + getChartView().getHeight());
//        RectF rectF = new RectF();
//        rectF.left = 0;
//        rectF.top = getChartView().getHeight() - Utils.convertDpToPixel(20);
//        rectF.right = getChartView().getWidth();
//        rectF.bottom = getChartView().getHeight();
//        canvas.drawRect(rectF, paint);
        super.draw(canvas, posX - getWidth() / 2f, getChartView().getHeight() - Utils.convertDpToPixel(30));
    }
}
